// 引入公共css
require("../../assets/css/normalize.css");
require("../../assets/css/reset.css");
require("../../assets/css/basic.css");
require("../../assets/css/property.css");
require("../../assets/fonts/word-img/iconfont.css");
// require("");
// 引入自己的
require("./sporting.less");
let axios = require("../../assets/js/axios.js");
let dom = require("../../assets/js/util.js");

window.addEventListener("DOMContentLoaded", function () {
	// 跳转
	dom.turnToPage("maping", ".icon-map2");
	var p1 = [116.434027, 39.941037];
	var p2 = [116.461665, 39.941564];
	// 定位插件
	AMap.plugin("AMap.Geolocation", function () {
		var geolocation = new AMap.Geolocation({
			// 是否使用高精度定位，默认：true
			enableHighAccuracy: true,
			// 设置定位超时时间，默认：无穷大
			timeout: 10000,
			// 定位按钮的停靠位置的偏移量
			offset: [10, 20],
			//  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
			zoomToAccuracy: true,
			//  定位按钮的排放位置,  RB表示右下
			position: "RB",
		});

		geolocation.getCurrentPosition(function (status, result) {
			if (status == "complete") {
				onComplete(result);
				console.log("获取定位成功");

				// 经纬度
				p0 = [result.position.KL, result.position.kT];
				console.log(p0);
			} else {
				onError(result);
				console.log("获取定位失败");
			}
		});

		function onComplete(data) {
			// data是具体的定位信息
		}

		function onError(data) {
			// 定位出错
		}
	});

	// 计算路径距离
	var p1 = [116.434027, 39.941037];
	var p2 = [116.461665, 39.941564];
	// 返回 p1 到 p2 间的地面距离，单位：米
	var dis = AMap.GeometryUtil.distance(p1, p2);
	console.log(dis);

	// 获取type
	let type = location.search.split("=").pop();

	// 计时,进入开始
	let count = 0;
	// 计时器id
	let timer = null;
	// 卡路里
	let calorie = 0;
	// 运动公里数
	let meter = 0;
	let speed = 0;
	// 速度
	function speedRandom(min, max) {
		return Math.floor(Math.random() * (max - min + 1)) + min;
	}

	// 计时
	timer = setInterval(function () {
		//随机速度
		speed = speedRandom(1.75, 3);
		count++;
		calorie = Math.ceil(count * 1.5);
		meter = Number((count * 1.75).toFixed(2));

		// 渲染
		dom.$("#hours").textContent = dom.zero(parseInt(count / 60 / 60));
		dom.$("#minutes").textContent = dom.zero(parseInt((count / 60) % 60));
		dom.$("#seconds").textContent = dom.zero(count % 60);
		dom.$("#kcal").textContent = calorie;
		dom.$("#meter").textContent = meter;
		dom.$("#speed").textContent = speed + "M/s";
	}, 1000);
	// 暂停
	dom.$("#pause").addEventListener("click", function () {
		dom.$("#pause").style.display = "none";
		dom.$("#continue").style.display = "block";

		// 计时器终止
		clearInterval(timer);
	});
	// 继续
	dom.$(".icon-continue2").addEventListener("click", function () {
		dom.$("#pause").style.display = "block";
		dom.$("#continue").style.display = "none";
		timer = setInterval(function () {
			//随机速度
			speed = speedRandom(1.75, 3);
			count++;
			calorie = Math.ceil(count * 1.5);
			meter = Number((count * 1.75).toFixed(2));

			// 渲染
			dom.$("#hours").textContent = dom.zero(parseInt(count / 60 / 60));
			dom.$("#minutes").textContent = dom.zero(parseInt((count / 60) % 60));
			dom.$("#seconds").textContent = dom.zero(count % 60);
			dom.$("#kcal").textContent = calorie;
			dom.$("#meter").textContent = meter;
			dom.$("#speed").textContent = speed + "M/s";
		}, 1000);
	});
	// 结束

	dom.$(".icon-over").addEventListener("click", function () {
		// 清除计时器
		clearInterval(timer);
		// 上传数据
		console.log("结束--duration", count);
		console.log("结束--calorie", calorie);
		console.log("结束--meter", meter);
		console.log("结束--speed", speed);
		// 发请求
		axios
			.post("/api/exercise/save", {
				type,
				duration: count,
				calorie,
				meter,
			})
			.then((res) => {
				console.log(res);
			});
		// 归零
		count = 0;
		calorie = 0;
		meter = 0;
		// 数据归零
		// 渲染
		dom.$("#hours").textContent = "00";
		dom.$("#minutes").textContent = "00";
		dom.$("#seconds").textContent = "00";
		dom.$("#kcal").textContent = calorie;
		dom.$("#meter").textContent = meter;

		// 跳转并且传递参数
		location.href = `maping.html?type=${type}`;
	});
});
